// pages/arList/arList.js
import {
  productList,
  untieCode
} from "../../server/api/common.js";

Page({
  data: {
    arList: [],
    statusBarHeight: 0, // 状态栏高度
    navBarHeight: 44, // 导航栏高度
    isCode: false, // 是否是通过二维码进入到该页面的
  },

  onLoad(options) {
    console.log(options,"111");
    this.getStatusBarHeight();
    if (options.q) {
      this.setData({ isCode: true });
      wx.navigateTo({
        url: "/pages/index/index?q=" + options.q,
      })
    }
  },
  onHide() {
    this.setData({isCode:false});
    console.log(this.data.isCode,'isCode');
  },
  getStatusBarHeight() {
    const that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          statusBarHeight: res.statusBarHeight,
          navBarHeight: res.statusBarHeight + 44 // 44为导航栏高度
        });
      }
    });
  },

  onShow() {
    console.log("触发",this.data.isCode);
    if(!this.data.isCode) {
      this.initData();
    }
  },

  initData() {
    const that = this;
    productList().then(res => {
      that.setData({
        arList: res.data.data
      });
      console.log(this.data.arList)
    });
  },

  autoBack() {
    if (this.data.isCode) {
      wx.reLaunch({
        url: '/pages/home/home',
      })
    } else {
      wx.navigateBack({
        delta: 1
      });
    }
  },

  relieve(e) {
    const code = e.currentTarget.dataset.code;
    wx.showModal({
      content: "解绑后您将不能体验该产品的AR效果，确定解绑么？",
      success: (res) => {
        if (res.confirm) {
          untieCode(code).then(res => {
            if (res.data.code === 0) {
              wx.showToast({
                title: "解绑成功！",
                icon: 'none'
              });
              this.initData()
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              });
            }
          });
        } else {
          wx.showToast({
            title: "您已取消解绑！",
            icon: 'none'
          });
        }
      }
    });
  },

  addShop() {
    wx.navigateTo({
      url: "/pages/queryBinding/queryBinding"
    });
  },

  goAr(e) {
    const item = e.currentTarget.dataset.item;
    if (item.productStatus == 0) {
      wx.showModal({
        content: "授权已过期，请联系商家！",
        showCancel: false,
        confirmText: "关闭",
      });
    } else {
      // 根据需要传递参数到目标页面
      wx.navigateTo({
        url: `/pages/index/index?item=${JSON.stringify(item)}`
      });
    }
  }
});